package com.wooming.studentinfosystem.controller;

import com.wooming.studentinfosystem.dto.StudentInfoDTO;
import com.wooming.studentinfosystem.service.impl.StudentUpdateService;
import com.wooming.studentinfosystem.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Controller层: 更新学生信息
 *
 * @author Wooming
 */
@RestController
@RequestMapping("/api/students")
public class StudentUpdateController {

    @Autowired
    private StudentUpdateService studentUpdateService;

    @PutMapping("/{id}")
    public Result<?> updateStudent(
            @PathVariable String id,
            @RequestBody StudentInfoDTO params
    ){
        // 验证学生ID是否存在
        if(!studentUpdateService.isStudentIdExists(id)){
            return Result.error(404, "未找到该学生");
        }

        // 验证更新后的ID是否已经存在
        if(!(id.equals(params.getStudentId())) && studentUpdateService.isStudentIdExists(params.getStudentId())){
            return Result.error(400, "更新后的学生ID已经存在");
        }

        // 更新学生信息
        studentUpdateService.updateStudent(id, params);
        return Result.success(null);
    }
}
